﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmCongTy_Edit : FormBase
    {
        public frmCongTy_Edit()
        {
            InitializeComponent();
        }

        public CongTy ItemEdit { get; set; }
        public int? LinhVucID { get; set; }
        public DiaChi DiaChiEdit { get; set; }
        public int? TinhThanhID { get; set; }
        public int? QuanHuyenID { get; set; }

        private void frmCongTy_Edit_Load(object sender, EventArgs e)
        {
            try
            {
                if (LinhVucID == null)
                {
                    cboLinhVuc.ItemIndex = 0;
                    if (cboLinhVuc.EditValue != null)
                        LinhVucID = Convert.ToInt32(cboLinhVuc.EditValue);
                }
                txtTen.DataBindings.Add(new Binding("Text", ItemEdit, "TenCongTy"));
                cboLinhVuc.DataBindings.Add(new Binding("EditValue", this, "LinhVucID", true));

                txtToaNha.DataBindings.Add(new Binding("Text", DiaChiEdit, "ToaNha"));
                txtSoNha.DataBindings.Add(new Binding("Text", DiaChiEdit, "SoNha"));
                txtTenDuong.DataBindings.Add(new Binding("Text", DiaChiEdit, "TenDuong"));
                txtPhuongXa.DataBindings.Add(new Binding("Text", DiaChiEdit, "PhuongXa"));
                LoadTinhThanh();
                if (TinhThanhID != null)
                    cboTinhThanh.EditValue = TinhThanhID;
                else
                    cboTinhThanh.ItemIndex = 0;
                LoadQuanHuyen();
                if (QuanHuyenID != null)
                    cboQuanHuyen.EditValue = QuanHuyenID;
                else
                    cboQuanHuyen.ItemIndex = 0;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void LoadTinhThanh()
        {
            var query = Session.DefaultSession.Query<TinhThanh>().Select(c => new
            {
                c.Oid,
                c.TenTinhThanh
            });
            cboTinhThanh.Properties.DisplayMember = "TenTinhThanh";
            cboTinhThanh.Properties.ValueMember = "Oid";
            cboTinhThanh.Properties.DataSource = query;
            if (query.Count() > 0)
                cboTinhThanh.ItemIndex = 0;
        }

        private void LoadQuanHuyen()
        {
            int id = Convert.ToInt32(cboTinhThanh.EditValue);
            var query = Session.DefaultSession.Query<QuanHuyen>().Where(c => c.TinhThanh.Oid == id).Select(c => new
            {
                c.Oid,
                c.TenQuanHuyen
            });
            cboQuanHuyen.Properties.DisplayMember = "TenQuanHuyen";
            cboQuanHuyen.Properties.ValueMember = "Oid";
            cboQuanHuyen.Properties.DataSource = query;
            if (query.Count() > 0)
                cboQuanHuyen.ItemIndex = 0;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {
                    TinhThanhID = Convert.ToInt32(cboTinhThanh.EditValue);
                    QuanHuyenID = Convert.ToInt32(cboQuanHuyen.EditValue);
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void cboTinhThanh_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                LoadQuanHuyen();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private bool CheckInput()
        {
            bool result = true;
            dxErrorProvider1.ClearErrors();
            /*
            if (txtMa.Text == string.Empty)
            {
                dxErrorProvider1.SetError(txtMa, "Chưa nhập");
                result = false;
            }
            else if (Session.DefaultSession.Query<CongTy>().FirstOrDefault(c => c.Oid != ItemEdit.Oid && c.MaCongTy == txtMa.Text) != null)
            {
                dxErrorProvider1.SetError(txtMa, "Mã đã sử dụng");
                result = false;
            }
             */
            if (LinhVucID == null)
            {
                dxErrorProvider1.SetError(cboLinhVuc, "Chưa chọn");
                result = false;
            }
            if (cboTinhThanh.EditValue == null)
            {
                dxErrorProvider1.SetError(cboTinhThanh, "Chưa chọn");
                result = false;
            }
            if (cboQuanHuyen.EditValue == null)
            {
                dxErrorProvider1.SetError(cboQuanHuyen, "Chưa chọn");
                result = false;
            }
            return result;
        }
    }
}